package com.zztl.infiniteBand.presenter

import android.content.Context
import com.zztl.infiniteBand.base.BasePresenter
import com.zztl.infiniteBand.model.ForceValueBean
import com.zztl.infiniteBand.model.MiningBean
import com.zztl.infiniteBand.model.NoDataResponseBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.FilterSubscriber2
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.requestBean.MiningReceiveRequest
import com.zztl.infiniteBand.net.requestBean.SyncStepRequest
import com.zztl.infiniteBand.net.requestBean.UnbindDeviceRequest
import com.zztl.infiniteBand.net.requestBean.UploadHealthDataRequest
import com.zztl.infiniteBand.ui.view.mvpview.MiningView
import com.zztl.infiniteBand.util.DataFormatUtil
import rx.Subscriber


/**
 * @desc
 * @auth ${user}
 * @time 2018/6/15  11:31
 */
class MiningPresenter : BasePresenter<MiningView>() {
    var mSubscriber: Subscriber<MiningBean>? = null

    fun getMiningData(imei: String?, context: Context, isFirst: Boolean) {
        if (isFirst) {
            mSubscriber = object : FilterSubscriber<MiningBean>(context) {
                override fun success(miningBean: MiningBean) {
                    view!!.miningSuccess(miningBean)
                }


                override fun fail() {
                    super.fail()
                }
            }
        } else {

            mSubscriber = object : FilterSubscriber2<MiningBean>(context) {
                override fun success(miningBean: MiningBean) {
                    view!!.miningSuccess(miningBean)
                }

                override fun fail(msg: String?) {

                }
            }
        }
        if (null != imei) {
            addSubscribe(RetorfitServiceManger
                    .observe(RetorfitServiceManger.instance!!.apiService
                            .miningDetail(DataConverter.instance.toJson(UnbindDeviceRequest(imei))))
                    .subscribe(mSubscriber))
        }
    }


    fun receiveAward(emsIdList: List<String>?, isAll: Boolean, context: Context) {
        if (null != emsIdList) {
            addSubscribe(RetorfitServiceManger
                    .observe(RetorfitServiceManger.instance!!.apiService
                            .miningReceive(DataConverter.instance.toJson(MiningReceiveRequest(DataFormatUtil.listToString(emsIdList)))))
                    .subscribe(object : FilterSubscriber2<NoDataResponseBean>(context) {
                        override fun success(bean: NoDataResponseBean) {
                            if (isAll) {
                                view!!.receiveAllSuccess()
                            } else {
                                view!!.receiveSuccess()
                            }
                        }

                        override fun fail(msg: String?) {
                            view!!.receiveFail()
                        }
                    }))
        }
    }

    fun syncStep(request: SyncStepRequest, context: Context) {
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService.syncStep(DataConverter.instance.toJson(request)))
                .subscribe(object : FilterSubscriber2<NoDataResponseBean>(context) {


                    override fun success(bean: NoDataResponseBean) {
                        view!!.syncStepSuccess(true,request.mileageSum)
                    }

                    override fun fail(message: String?) {
                        view!!.syncStepSuccess(false, request.mileageSum)
                    }


                }))
    }


    fun uploadHealthData(request: UploadHealthDataRequest) {
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService.uploadHealthData(DataConverter.instance.toJson(request)))
                .subscribe(object : FilterSubscriber2<NoDataResponseBean>(null) {
                    override fun success(bean: NoDataResponseBean) {
                    }

                    override fun fail(msg: String?) {
                    }

                }))
    }


    fun getForceValue(isFirst: Boolean, context: Context) {
        val subscriber = object : FilterSubscriber<ForceValueBean>(context) {
            override fun success(t: ForceValueBean) {
                view!!.getFroceValueSuccess(t)
            }
        }

        val subscriber2 = object : FilterSubscriber2<ForceValueBean>(context) {
            override fun fail(message: String?) {
            }

            override fun success(t: ForceValueBean) {
                view!!.getFroceValueSuccess(t)
            }
        }

        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService.getForceVlue(DataConverter.instance.toJson("")))
                .subscribe(if (isFirst) subscriber else subscriber2))


    }
}
